<?php
namespace haibao\cms\business\control;

class PagerController extends \haibao\frame\http\UserControl{
	
	private $pageCount = 5;
	public function __construct(){
		$this->setConfig( 'default' );
		//parent::__construct('/control/pager.html');
	}
	
	protected function preRender(){
		$pager = $this->getParameter('pager');
		if($this->getParameter('pageSize')){
			//$this->pageCount = $this->getParameter('pageSize');
		}
		if ($pager){
			
			$currentPage = $pager->getCurrentPage();
			$totalPage = $pager->getPageCount();

			$prePage = $currentPage - 1;
			$nextPage = $currentPage + 1;
			
			$preDisable = $nextDisable = false;
			
			if($currentPage == 1){
				$preDisable = true;
			}

			if($currentPage >= $totalPage){
				$nextDisable = true;
			}
				
			if ($prePage < 1){
				$prePage = 1;
			}
			if ($nextPage > $totalPage){
				$nextPage = $totalPage;
			}
		
			$pageStart = $currentPage - 1;
			$pageEnd = $currentPage + 3;
			if ($pageStart < 1){
				$pageEnd = 1 - $pageStart + $pageEnd;
				if ($pageEnd > $totalPage){
					$pageEnd = $totalPage;
				}
				$pageStart = 1;
			}
			if ($pageEnd > $totalPage){
				$pageStart = $pageStart - ($pageEnd - $totalPage);
				if ($pageStart < 1){
					$pageStart = 1;
				}
				$pageEnd = $totalPage;
			}

			
			$this->assign('preDisable', $preDisable);
			$this->assign('nextDisable', $nextDisable);
			$this->assign('currentPage',$currentPage);
			$this->assign('prePage',$prePage);
			$this->assign('nextPage',$nextPage);
			$this->assign('totalPage',$totalPage);
			$this->assign('pages',range($pageStart,$pageEnd));
			$this->assign('pageStart',$pageStart);
			$this->assign('pageEnd',$pageEnd);
			$this->assign('pageCount',$this->pageCount);
			
			$viewName = $this->getParameter('viewName') ? $this->getParameter('viewName') : \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::ARTICLE_VIEW_NAME);
			$isPriView = false;
			if($viewName == "priview"){
				$isPriView = true;
			}
			$this->assign('id',$this->getParameter('siteTemplateId'));
			$this->assign('viewName',$viewName);
			$this->assign('isPriView',$isPriView);
			$this->assign('pageUrl', $this->getParameter('pageUrl'));
			$this->setTemplateFile("/control/controlpager.html");
		}
	}
}